package com.advanced.commonlyUsedClass_2.packaging;

import java.util.Random;
import java.util.Scanner;

/**
 * 在控制台上输入一段字符(长度不小于 7，并且不重复的字符长度不小于 7)，
 * 从中 随机抽取 7 个字符，要求抽取的字符不能重复，然后将抽取的字符输出。
 * 抽取的字符要求用 StringBuffer 存储。
 *
 * @author: Jovi
 * @createTime: 2017-06-06 15:37
 **/
public class ExtractiveString {
    public static void main(String[] args) {
        System.out.println("Please enter a string：");
        Scanner sc = new Scanner(System.in);
        String strs = sc.next();
        if (strs.length() < 7) {
            System.out.println("The input string length is less than 7");
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 7; i++) {
            Random random = new Random();
            int num = random.nextInt(strs.length());
            int index = sb.indexOf(strs.charAt(num) + "");
            if (index != -1) {
                i--;
            } else {
                sb.append(strs.charAt(num) + "，");
            }
        }
        System.out.println("A string is of extraction is:"+sb);
    }
}
